package com.dream.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 匠人码农
 * @date 2020/11/08 8:09
 * 概要：
 *      基底servlet类
 */

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //解决请求乱码问题
        req.setCharacterEncoding("utf-8");

        //解决浏览器中文乱码的问题
        resp.setContentType("text/html;charset=utf-8");

        //使用反射执行方法
        String action = req.getParameter("action");
        try{
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
